import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

/**
 *
 * @author pascal
 */
public class SerialTest {
    public static long[] generateTrie(){
        long[] trie = new long[27000];
        Random rand = new Random();
        for (int i = 0; i < trie.length; i++){
            trie[i] = rand.nextInt(27000);
        }
        return trie;
    }

    public static void writeTrie(long[] trie){
        try {
            FileOutputStream fout = new FileOutputStream("serialized_object.dat");
            ObjectOutputStream out = new ObjectOutputStream(fout);

            out.writeObject(trie);
            out.flush();
            out.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Could not find file: " + ex.getMessage());
        } catch (IOException e){
            System.out.println("IOException: " + e.getMessage());
        }
    }

    public static long[] readTrie(){
        long[] trie = null;
        try{
            FileInputStream fin = new FileInputStream("serialized_object.dat");
            ObjectInputStream in = new ObjectInputStream(fin);

            trie = (long[])in.readObject();
            in.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Could not find file: " + ex.getMessage());
        } catch (IOException e){
            System.out.println("IOException: " + e.getMessage());
        } catch (ClassNotFoundException e){
            System.out.println("ClassNotFoundException: " + e.getMessage());
        }
        return trie;
    }

    public static void printTrie(long[] trie){
        for (int i = 0; i < trie.length; i++){
            System.out.println("[" + i + "]: " + trie[i]);
        }
    }

    public static void main(String[] args){
        //long[] trie = SerialTest.generateTrie();
        //SerialTest.writeTrie(trie);
        long[] trie = SerialTest.readTrie();
        SerialTest.printTrie(trie);
    }
}
